package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.PayDetailDAO;
import com.daxia.bumall.dto.PayDetailDTO;
import com.daxia.bumall.model.PayDetail;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class PayDetailService {
	
	@Autowired
	private PayDetailDAO payDetailDAO;
	
	public List<PayDetailDTO> find(PayDetailDTO query, Page page) {
		List<PayDetail> models = payDetailDAO.find(query, page);
		List<PayDetailDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<PayDetailDTO> findAll() {
		return this.find(new PayDetailDTO(), null);
	}
	
	public List<PayDetailDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new PayDetailDTO(), page);
	}
	
	public Long create(PayDetailDTO dto) {
		PayDetail model = new PayDetail();
		toModel(model, dto);
		return payDetailDAO.create(model);
	}
	
	public PayDetailDTO load(Long id) {
	    PayDetail model = payDetailDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(PayDetailDTO dto) {
		PayDetail model = payDetailDAO.load(dto.getId());
		toModel(model, dto);
		payDetailDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				payDetailDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public PayDetailDTO findOne(PayDetailDTO query) {
		PayDetail model = payDetailDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<PayDetailDTO> toDTOs(List<PayDetail> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<PayDetailDTO>(0);
		}
		List<PayDetailDTO> dtos = new ArrayList<PayDetailDTO>(models.size());
		for (PayDetail model : models) {
	        PayDetailDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private PayDetailDTO toDTO(PayDetail model) {
		if (model == null) {
			return null;
		}
		PayDetailDTO dto = BeanMapper.map(model, PayDetailDTO.class);
		
		return dto;
	}
	
	private void toModel(PayDetail model, PayDetailDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<PayDetail> toModels(List<PayDetailDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<PayDetail>(0);
		}
		List<PayDetail> models = new ArrayList<PayDetail>(dtos.size());
		for (PayDetailDTO dto : dtos) {
	        PayDetail model = new PayDetail();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
